jq(document).ready(function(){
	try{
	
		function resetHeaderPosition() {

			var window_h = jq(window).height();
			var window_w = jq(window).width();
			
			var zzzzzz = jq("#zzzzzz");
			
			if(window_h < 400 || window_w < 980) {
				if(!zzzzzz.hasClass("tiny-view")) {
					zzzzzz.addClass("tiny-view");
				}
			}else {
				if(zzzzzz.hasClass("tiny-view")) {
					zzzzzz.removeClass("tiny-view");
				}
			}
			
		};
		
		var resize_timer = { timer : null, activate : false };

		jq(window).resize(function() {
			setTimer(resetHeaderPosition, 100, resize_timer);
		});
		
		jq('a[href="#close"]').click(function(e) {
  		try{
			e.preventDefault();
			jq(this).parent().hide();
		}catch(err) {
			alert(err);
		}
		});
		
		/* ------------------------------ setTimer and clearTimer ------------------------------ */
		
		function setTimer(fct, delay, timer_obj) {
			if(timer_obj.activate) {
				clearTimer(timer_obj);
			}
			timer_obj.timer = setTimeout(function() { fct(); timer_obj.activate = false; }, delay);
			timer_obj.activate = true;
		}
	
		function clearTimer(timer_obj) {
			clearTimeout(timer_obj.timer);
			timer_obj.activate = false;		
		}
		
		/* ------------------------------ language change ------------------------------ */
    	
		jq("#language").change(function() {
		try{
  			var url = window.location.href;
			var tempArray = url.split("?");
			var baseURL = tempArray[0];
			var aditionalURL = tempArray[1];
			var newAdditionalURL = "";
			var temp = "";
			if(aditionalURL){
				var tempArray = aditionalURL.split("&");
				for ( var i in tempArray ) {
        			if(tempArray[i].indexOf("lang") == -1){
                		newAdditionalURL += temp+tempArray[i];
                		temp = "&";
                	}
            	}
			}
			var lang_txt = temp + "lang=" + jq("#language").val();
			var finalURL = baseURL + "?" + newAdditionalURL + lang_txt;
			window.location.href = finalURL;
			
  		}catch(err) {
			alert(err);
		}
		});
		
		
		/* ------------------------------ back to top ------------------------------ */
		
		var j_auto_scroll = true;
		var j_back_to_top = jq("#back_to_top");
		
		function backtotop_scroll() {
		try{
			var scroll_top = jq(window).scrollTop();

			if(scroll_top > 50) {
				j_back_to_top.css({'display': 'block'});
				j_back_to_top.animate({'opacity': '0.7'}, 400);
			} else if(scroll_top <= 50) {
				j_back_to_top.animate({'opacity': '0'}, 400);
				setTimeout(function() { j_back_to_top.css({'display': 'none'}); }, 410);
			}
		}catch(err) {
			alert(err);
		}
		}
		
		function backToTopClick() {
			var scroll_top = jq(window).scrollTop();
			var time = scroll_top * 0.3;
			jq("html, body").animate({ scrollTop: 0 }, time);
		}
		
		if(j_auto_scroll) {
			jq(window).scroll(jq.throttle(450, backtotop_scroll));
		}
		
		j_back_to_top.click(backToTopClick);
		
		// Reset header position when the DOM is fully loaded
		resetHeaderPosition();	

		
		/* ------------------------------ check user signed or not ------------------------------ */
		
		
		
		
		
		/* ------------------------------ some useful function ------------------------------ */
		
		// pure javascript
		function getParameterByName(name) {
			name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		  	var regexS = "[\\?&]" + name + "=([^&#]*)";
		  	var regex = new RegExp(regexS);
		  	var results = regex.exec(window.location.search);
		  	if(results == null) {
		  		return "";
		  	}else {
		    	return decodeURIComponent(results[1].replace(/\+/g, " "));
		    }
		}
		
		// use jquery
		function getURLParameter(name) {
    		return decodeURI(
        		(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    		);
    	}
		
		
	}catch(err) {
		alert(err);
	}
});
